<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <!-- 列表 -->
    <ContentWrap>
      <el-table ref="multipleTableRef" :data="list" style="width: 100%; margin-top: 10px; height: 300px;">
        <el-table-column label="序号" type="index"  header-align="center" align="center" width="60px" fixed />
        <el-table-column label="任务名称" align="center" prop="nickname" show-overflow-tooltip>
          <template #default="scope">
            {{ taskList.find((dept) => dept.id === scope.row.dffTaskId)?.dtName }}
          </template>
        </el-table-column>
        <el-table-column label="表单名称" align="center" key="deptName" prop="deptName" show-overflow-tooltip>
          <template #default="scope">
            {{ formList.find((dept) => dept.id === scope.row.dffFormId)?.dfName }}
          </template>
        </el-table-column>
        <el-table-column label="填报数据" align="center" prop="dffFillData" width="360" show-overflow-tooltip />
        <el-table-column label="填报人" align="center" prop="dffFillUserId" width="120">
          <template #default="scope">
            {{ userList.find((user) => user.id === scope.row.dffFillUserId)?.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="填报时间" align="center" prop="createTime" width="180" :formatter="dateFormatter" />
        <el-table-column label="操作" align="center" fixed="right" width="140px">
          <template #default="scope">
            <el-button
              size="small"
              link
              type="danger"
              @click.prevent="openForm(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <TaskFillLogDetail ref="fillLogDetailRef" />
  </Dialog>
</template>
<script setup lang="ts">
import { FormApi, FormVO, FormTaskFillVO } from '@/api/system/form'
import { TaskApi, TaskVO } from '@/api/system/task'
import * as UserApi from '@/api/system/user'

import  TaskFillLogDetail from './TaskFillLogDetail.vue'

import { DICT_TYPE } from '@/utils/dict'
import { getStrDictOptions } from '@/utils/dict'
import { CommonStatusEnum } from '@/utils/constants'
import { dateFormatter } from '@/utils/formatTime'

/** 表单任务 表单 */
defineOptions({ name: 'TaskForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const dffTaskId = ref(0)

const loading = ref(true) // 列表的加载中
const list = ref<FormTaskFillVO[]>([]) // 列表的数据
const formList = ref<FormVO[]>([]) // 列表的数据
const taskList = ref<TaskVO[]>([]) // 列表的数据
const userList = ref<UserApi.UserVO[]>([]) // 用户列表
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dffTaskId: undefined,
  dffFormId: undefined
})

/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '数据查看'
  loading.value = true
  try {
    queryParams.dffTaskId = id
    dffTaskId.value = id
    const data = await FormApi.getTaskFillPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
  formList.value = await FormApi.getSimpleFormList()
  taskList.value = await TaskApi.getSimpleTaskList()
  // 获取用户列表
  userList.value = await UserApi.getSimpleUserList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.dffTaskId = dffTaskId.value
    const data = await FormApi.getTaskFillPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const fillLogDetailRef = ref()
const openForm = async (data: FormTaskFillVO) => {
  fillLogDetailRef.value.open(data)
}

</script>